package com.ruoyi.jihuo.quzrtz;


import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.jihuo.api.CallTaoApiService;
import com.ruoyi.jihuo.domain.JhTmAccount;
import com.ruoyi.jihuo.domain.JhTmCategorySchema;
import com.ruoyi.jihuo.mapper.JhTmCategorySchemaMapper;
import com.ruoyi.jihuo.service.IJhTmAccountService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.stream.Collectors;

@Component
public class SyncJhTmCategory {
    private static final Logger log = LoggerFactory.getLogger(SyncJhTmCategory.class);

    @Autowired
    private IJhTmAccountService jhTmAccountService;

    @Autowired
    private CallTaoApiService callTaoApiService;

    @Autowired
    private JhTmCategorySchemaMapper jhTmCategorySchemaMapper;

    public void sync() {
        Map<Long, JhTmAccount> accountMap = jhTmAccountService.selectAllJhTmAccountList().stream()
                .collect(Collectors.toMap(JhTmAccount::getJhId, account -> account));
        for (JhTmCategorySchema jhTmCategorySchema : jhTmCategorySchemaMapper.selectAllJhTmAccountList()) {
            JhTmAccount jhTmAccount = accountMap.get(jhTmCategorySchema.getJhTmAccountId());
            String schema = callTaoApiService.getAddProductSchema(jhTmAccount, jhTmCategorySchema.getJhCatId());
            jhTmCategorySchema.setJhCatSchema(schema);
            log.info("店铺 {} 分类 {} 同步分类完成：{}", jhTmAccount.getJhName(), jhTmCategorySchema.getJhCatName(), schema);
            jhTmCategorySchemaMapper.updateById(jhTmCategorySchema);
        }
    }
}
